import { TIME_STAMP, TOKEN_TIME_VALUE } from '../const'
import { getItem, setItem } from './storage'

/**
 * 获取时间戳
 */
export function getTimeStamp () {
  return getItem(TIME_STAMP)
}
/**
 * 设置时间戳
 */
export function setTimeStamp () {
  setItem(TIME_STAMP, Date.now())
}
/**
 * 是否超时
 */
export function isCheckTimeout () {
  // 当前时间
  const currentTime = Date.now()
  // 缓存时间
  const timeStamp = getTimeStamp()

  return currentTime - timeStamp > TOKEN_TIME_VALUE
}

/**
 * 封装用户权限
 */
export function initPermission (roles) {
  const permissions = []
  if (roles === []) return permissions
  roles.forEach(element => {
    permissions.push(element.code)
    element.roleAuthorizes.forEach(authorize => {
      permissions.push(authorize.authorize.code)
    })
  })
  return permissions
}
